﻿using UnityEngine;   
using System.Collections;   
using System.Xml;   
using System.Xml.Serialization;   
using System.IO;   
using System.Text;   

public class FModelBase{
	public delegate void CallBack();
	public static event CallBack onSaveAll;
	public static event CallBack onLoadAll;
	private static DGameData instance;
	public static DGameData Instance{
		get{
			if(instance == null)
				instance = new DGameData();
			return instance;
		}
	}
	public static void LoadAll(){
		if (onLoadAll != null)
			onLoadAll ();
	}
	public static void SaveAll(){
		if (onSaveAll != null)
			onSaveAll ();	
	}
}
public interface IModelBase{
	IModelBase Load();
	void Save();
	void CheckData();
}
[System.Serializable]
public class ModelBase:IModelBase{
	protected string name = "";
	public void setName(string name){
	
	}
	public virtual void CheckData (){
	}
	protected virtual System.Type getType(){
		return GetType();
	}
	public virtual IModelBase Load(){
		return FModelBase.Instance.Load(getType().Name+name,getType());
	}
	public virtual void Save(){
		FModelBase.Instance.Save(getType().Name+name,this,getType());
	}
}



public class DGameData{
	private FileInfo getFileInfo(string name){
		FileInfo f ;  
		f = new FileInfo(Application.persistentDataPath+"/"+"V"+_version+"_"+ name+".xml");
		return f;
	}
	  
	public string _version = "0";
	public IModelBase Load(string pathname,System.Type type)   
	{   
		string _data;
		Debug.Log ("----LoadPath:" + (Application.persistentDataPath+"/"+pathname));
		FileInfo f = getFileInfo (pathname);
		if(f.Exists)   
		{   
			StreamReader r = f.OpenText();   
			string _info = r.ReadToEnd();   
			r.Close();   
			_data=_info;   
			if(_data.ToString() != "")   
			{   
				IModelBase data =DeserializeObject(_data,type);
				return data;
			}   
		}
		Debug.Log ("No Find"+pathname);
		return null;
	}   

	public void Save(string pathname,IModelBase target,System.Type type)  
	{      
		string _data;
		Debug.Log ("----SavePath:" + (Application.persistentDataPath+"/"+pathname));
		_data = SerializeObject(target,type);   
		StreamWriter writer;   
		FileInfo f = getFileInfo (pathname);
		if(f.Exists)   
		{   
			f.Delete();    
		}      
		writer = f.CreateText();   
		writer.Write(_data);   
		writer.Close();   
	}     
	string UTF8ByteArrayToString(byte[] characters)   
	{        
		UTF8Encoding encoding = new UTF8Encoding();   
		string constructedString = encoding.GetString(characters);   
		return (constructedString);   
	}   
	
	byte[] StringToUTF8ByteArray(string pXmlString)   
	{   
		UTF8Encoding encoding = new UTF8Encoding();   
		byte[] byteArray = encoding.GetBytes(pXmlString);   
		return byteArray;   
	}   
	
	// Here we serialize our Reward object of reward   
	string SerializeObject(object pObject,System.Type type)   
	{  
		string XmlizedString = null;   
		MemoryStream memoryStream = new MemoryStream();   
		XmlSerializer xs = new XmlSerializer(type);   
		XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);   
		xs.Serialize(xmlTextWriter, pObject);   
		memoryStream = (MemoryStream)xmlTextWriter.BaseStream;   
		XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());   
		return XmlizedString;   
	}   
	
	// Here we deserialize it back into its original form   
	IModelBase DeserializeObject(string pXmlizedString,System.Type type)   
	{   
		XmlSerializer xs = new XmlSerializer(type);   
		MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));   
//		XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);   
		return xs.Deserialize(memoryStream) as IModelBase;   
	}   
}
